package com.un.ebs.handler;

import com.un.ebs.businessObject.service.SQLStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zc
 * @ClassName HandlerDbContext
 * @Description 根据对应数据库类型获取处理策略
 * @Date 2021/1/5
 */
@Component
public class HandlerDbContext {
    @Autowired
    private ApplicationContext applicationContext;

    //存放所有策略类Bean的map
    public static Map<String, Class<SQLStrategy>> dbStrategyBeanMap = new HashMap<>();

    public SQLStrategy getDbCommonStrategy(String type) {
        Class<SQLStrategy> strategyClass = dbStrategyBeanMap.get(type);
        if (strategyClass == null) {
            throw new IllegalArgumentException("没有对应的处理策略");
        }
        //从容器中获取对应的策略Bean
        return applicationContext.getBean(strategyClass);
    }

}
